﻿using MSSClient.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace MSSClient
{
    /// <summary>
    /// Interaction logic for RenameDlg.xaml
    /// </summary>
    public partial class ChangeWebSiteDlg : Window
    {

        public int changTag = 0;
        public delegate void ChangeWebSiteDlgDelegate();
        public ChangeWebSiteDlgDelegate changeWebSiteDlg;

        public ChangeWebSiteDlg(string renameText)
        {
            InitializeComponent();
            now_site.Content = renameText;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (newSite.Text.Trim() == "")
            {
                MessageBox.Show("新地址不能为空！");
                return;
            }
            string configPath = System.AppDomain.CurrentDomain.BaseDirectory;
            //MessageBox.Show(configPath);
            XmlDocument MyXml = new XmlDocument();
            MyXml.Load(configPath+ "MSSClient.exe.config");
            //获取<configuration>节点的所有子节点
            XmlNodeList MyNodeList = MyXml.SelectSingleNode("configuration").ChildNodes;

            //遍历<configuration>下的所有子节点
            foreach (XmlNode MyNode in MyNodeList)
            {
                if (MyNode.Name == "appSettings")
                {
                    XmlNodeList InfoPageNodeList = MyNode.ChildNodes;
                    //遍历InfoPage节点
                    foreach (XmlNode InfoPageNode in InfoPageNodeList)
                    {
                        if (InfoPageNode.Attributes["key"].InnerXml == "strUrl")
                        {
                            InfoPageNode.Attributes["value"].InnerXml = newSite.Text.Trim();
                            now_site.Content = newSite.Text.Trim();
                            MyXml.Save(configPath + "MSSClient.exe.config"); //保存
                            changTag = 1;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        private void button_close_Click(object sender, RoutedEventArgs e)
        {
            if (newSite.Text.Trim() != "" || changTag.Equals(1))
                  changeWebSiteDlg();
            this.Close();
        }

        private void new_site_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            CommonUtil.ShowMsgFrom("内容:[" + now_site.Content + "]已经复制到剪贴板");
            Clipboard.SetDataObject(now_site.Content);
        }
    }
}
